package dump;

import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;

import process.Result;
import process.ResultProcessManager;

public class DumpManager {

	/**
	 * 
	 */
	public static void dumpResults() {
		int i, j, limit;
		String runtag = "plnbst031ms";
		
		DateFormat dateFormat = new SimpleDateFormat("dd_MM_yyyy-HH_mm_ss");
		Date date = new Date();
		

		String resultsPath = "./answers_" + dateFormat.format(date) + ".txt";

		String answerString;

		
		try{
			// Create file 
			FileWriter fstream = new FileWriter(resultsPath);
			BufferedWriter out = new BufferedWriter(fstream);

			ArrayList<ArrayList<Result>> allResults = ResultProcessManager.getResults();
			ArrayList<Result> particularResults;
			
			//Write stuff
			for (i = 0; i < allResults.size() ; i++) {
				particularResults = allResults.get(i);
				
				if (particularResults.size() > 3)
					limit = 3;
				else
					limit = particularResults.size();
				
				if (limit > 0) {
					for (j = 0; j < limit; j++) {
						answerString = particularResults.get(j).getQuery().getQuestionAnalysis().getIdentifier() + 
								" " + runtag + " " + (j+1)  + " " + particularResults.get(j).getScore() + " " +
								particularResults.get(j).getDocID() + " " + particularResults.get(j).getAnswer() + "\n";
						System.out.println(answerString);
						out.write(answerString);
					}
				}
				

			}
			
			//Close the output stream
			out.close();

		} catch (Exception e){//Catch exception if any
			System.err.println("Error: " + e.getMessage());
		}
		
		
		
		
		
	}
}
